package com.egg.ylt.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.activity.ACT_ShopDetail;
import com.egg.ylt.adapter.ADA_CollectShopRecycler;
import com.egg.ylt.pojo.CollentShopEntity;
import com.egg.ylt.presenter.impl.CollectShopPresenterImpl;
import com.egg.ylt.view.ICollectShopView;
import com.yonyou.framework.library.base.BaseFragment;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.eventbus.EventCenter;

import butterknife.BindView;

/**
 * sichaoqi on 2018/12/14
 */
public class FRA_CollectShop extends BaseFragment<CollectShopPresenterImpl> implements ICollectShopView {
    @BindView(R.id.shop_recycler)
    RecyclerView shopRecycler;
    @BindView(R.id.rl_loading_view)
    RelativeLayout rlLoadingView;
    private ADA_CollectShopRecycler ada_shopRecycler;

    private String pageSize = "50";
    private String pageNum = "1";

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {
        if (mPresenter != null) {
            //请求收藏咨询列表数据  0是咨询  1 门店
            mPresenter.getNewsData(Constants.TOKEN, Constants.COMPANYID, "1", pageSize, pageNum);
        }
    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected View getLoadingTargetView() {
        return rlLoadingView;
    }

    @Override
    protected void initViewsAndEvents() {
        //初始化recycler
        initRecycler();

    }

    private void initRecycler() {
        //设置布局管理器
        shopRecycler.setLayoutManager(new LinearLayoutManager(mContext));

        //设置适配器
        ada_shopRecycler = new ADA_CollectShopRecycler(mContext);
        shopRecycler.setAdapter(ada_shopRecycler);

        //list的条目监听
        ada_shopRecycler.setOnItemClickListener(new ADA_CollectShopRecycler.OnItemClickListener() {
            @Override
            public void onItemClick(String id) {
                Intent mIntent = new Intent();
                mIntent.putExtra("id", id);
                readyGo(ACT_ShopDetail.class, mIntent);
            }
        });

        //请求收藏咨询列表数据  0是咨询  1 门店
        mPresenter.getNewsData(Constants.TOKEN, Constants.COMPANYID, "1", pageSize, pageNum);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fra_collect_shop;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    public void getNewsData(CollentShopEntity entity) {
        //得到资讯数据
        if (ListUtil.isListEmpty(entity.getList())) {
            shopRecycler.setVisibility(View.GONE);
            toggleShowEmpty(true, R.drawable.empty_no_response, "暂无门店信息");
            return;
        }

        toggleShowEmpty(false, -1, "");
        ada_shopRecycler.addList(entity.getList());
    }
}
